#include <stm8s_tim1.h>

void TIM1_PWMIConfig(TIM1_Channel_TypeDef TIM1_Channel,
                     TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
                     TIM1_ICSelection_TypeDef TIM1_ICSelection,
                     TIM1_ICPSC_TypeDef TIM1_ICPrescaler,
                     uint8_t TIM1_ICFilter)
{
  uint8_t icpolarity = TIM1_ICPOLARITY_RISING;
  uint8_t icselection = TIM1_ICSELECTION_DIRECTTI;
  
  /* Check the parameters */
  assert_param(IS_TIM1_PWMI_CHANNEL_OK(TIM1_Channel));
  assert_param(IS_TIM1_IC_POLARITY_OK(TIM1_ICPolarity));
  assert_param(IS_TIM1_IC_SELECTION_OK(TIM1_ICSelection));
  assert_param(IS_TIM1_IC_PRESCALER_OK(TIM1_ICPrescaler));
  
  /* Select the Opposite Input Polarity */
  if (TIM1_ICPolarity != TIM1_ICPOLARITY_FALLING)
  {
    icpolarity = TIM1_ICPOLARITY_FALLING;
  }
  else
  {
    icpolarity = TIM1_ICPOLARITY_RISING;
  }
  
  /* Select the Opposite Input */
  if (TIM1_ICSelection == TIM1_ICSELECTION_DIRECTTI)
  {
    icselection = TIM1_ICSELECTION_INDIRECTTI;
  }
  else
  {
    icselection = TIM1_ICSELECTION_DIRECTTI;
  }
  
  if (TIM1_Channel == TIM1_CHANNEL_1)
  {
    /* TI1 Configuration */
    TI1_Config((uint8_t)TIM1_ICPolarity, (uint8_t)TIM1_ICSelection,
               (uint8_t)TIM1_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
    
    /* TI2 Configuration */
    TI2_Config(icpolarity, icselection, TIM1_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
  }
  else
  {
    /* TI2 Configuration */
    TI2_Config((uint8_t)TIM1_ICPolarity, (uint8_t)TIM1_ICSelection,
               (uint8_t)TIM1_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
    
    /* TI1 Configuration */
    TI1_Config(icpolarity, icselection, TIM1_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
  }
}

/**
  * @brief  Enables or disables the TIM1 peripheral.
  * @param  NewState new state of the TIM1 peripheral.
  *         This parameter can be ENABLE or DISABLE.
  * @retval None
  */
